package TAIC.IPLM ;
import java.util.* ;
import java.io.* ;

class ConvertData {
	int totalVec = 0 , totalPic = 0 ; 
	int cluster = 0 ; 
	int feature[][] ;
	int classNo [ ] ;
	int kwNo [] ;
	
	static public void main ( String argu [] ) {
		if ( argu.length == 0 ){
			System.out.println ( "Please input the cluster size" ) ;
			return ; 
		}
		( new ConvertData( Integer.valueOf (argu [ 0 ]) )).convert ();
	}
	
	ConvertData ( int clu ) {
		cluster = clu ;
	}
	
	void convert (){
		int i , j,  oriPic , classes = 0 ;
		int totalKeys = 0 ; 
		
		try {
			Scanner cluResult = new Scanner ( new File ( "train.mat.clustering." + cluster ) ) ;
			Scanner oriClass = new Scanner ( new File ( "train.class" ) ) ; 
			
			classes = 0  ; 
			totalVec = oriClass.nextInt () ;
			totalPic = oriClass.nextInt () ; 
			feature = new int [ totalPic ][ cluster ] ; 
			classNo = new int [ totalPic ] ; 
			kwNo = new int [ totalPic ] ;  // key word no. 

			for ( i = 0 ; i < totalVec ;i ++ ) {
				int className = oriClass.nextInt () ;
				int clusterName = cluResult.nextInt () ;
				oriPic = oriClass.nextInt () ;
				kwNo [ oriPic ] = oriClass.nextInt() ; 
				classNo [ oriPic ] = className ;
				feature [ oriPic ][ clusterName ] ++ ; 
				if ( className > classes ) classes = className ; 
			}
			
			classes = Math.round ( (float)(classes+1)/2 ) ;
			
			PrintStream fout = new PrintStream ( new File ( "svmCorpus" ) ) ;
			fout.println ( totalPic + " " +  classes ) ;
			
			for ( i = 0; i < totalPic ; i ++ ) {
//				if ( kwNo [ i ] >= keys || curImage [ classNo[i] ][ kwNo[i] ] >= imageNo [ classNo[i] ][ kwNo[i] ]) continue ; 
//				curImage [ classNo[i] ][ kwNo[i] ] ++ ; 
				fout.print ( classNo [ i ] + " " + kwNo [ i ]) ;    
				for ( j = 0  ; j < cluster ; j ++ ) 
					if ( feature [ i ][ j ] != 0 ) fout.print ( " " + ( j+1 ) + ":" + feature [ i ][ j ] ) ;
				fout.println (); 
			}
			fout.close () ;
			
		} catch ( Exception e ) {
			e.printStackTrace () ; 
		}
	}
}